package com.example.blueconn.fragment;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;

import com.example.blueconn.Constant;
import com.example.blueconn.R;
import com.example.blueconn.data.LightData;
import com.example.blueconn.data.TempData;
import com.example.blueconn.databinding.FragmentShowDetailBinding;
import com.example.blueconn.manager.ChartMgr;
import com.example.blueconn.manager.DataMgr;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.LineData;

import java.util.Map;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link ShowDetailFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class ShowDetailFragment extends Fragment {

    public static final String TAG = "ShowDetailFragment";

    private int deviceNum = 0;

    private FragmentShowDetailBinding detailBinding;

    public ShowDetailFragment() {
        // Required empty public constructor
    }

    public static ShowDetailFragment newInstance(Bundle bundle) {
        ShowDetailFragment fragment = new ShowDetailFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        detailBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_show_detail, container, false);
        initChart();
        registerChartRefresh();
        initListener();
        return detailBinding.getRoot();
    }

    private void initListener() {
        detailBinding.lightImgA.setImageResource(R.drawable.ic_light_select);
        detailBinding.tempImgA.setImageResource(R.drawable.ic_temp_select);
        setDeviceNum(0, true);
        setDeviceNum(0, false);
        DataMgr.getRealLightData().observe(getViewLifecycleOwner(), lightData -> {
            detailBinding.lightA1.setText(String.valueOf(lightData.lightA1));
            detailBinding.lightA2.setText(String.valueOf(lightData.lightA2));
            detailBinding.lightB1.setText(String.valueOf(lightData.lightB1));
            detailBinding.lightB2.setText(String.valueOf(lightData.lightB2));
            detailBinding.lightC1.setText(String.valueOf(lightData.lightC1));
            detailBinding.lightC2.setText(String.valueOf(lightData.lightC2));
            detailBinding.lightD1.setText(String.valueOf(lightData.lightD1));
            detailBinding.lightD2.setText(String.valueOf(lightData.lightD2));
            detailBinding.lightE1.setText(String.valueOf(lightData.lightE1));
            detailBinding.lightE2.setText(String.valueOf(lightData.lightE2));
            detailBinding.lightF1.setText(String.valueOf(lightData.lightF1));
            detailBinding.lightF2.setText(String.valueOf(lightData.lightF2));
        });

        DataMgr.getRealTempData().observe(getViewLifecycleOwner(), tempData -> {
            detailBinding.tempA.setText(String.valueOf(tempData.temp1));
            detailBinding.tempB.setText(String.valueOf(tempData.temp2));
            detailBinding.tempC.setText(String.valueOf(tempData.temp3));
            detailBinding.tempD.setText(String.valueOf(tempData.temp4));
            detailBinding.tempE.setText(String.valueOf(tempData.temp5));
        });

        detailBinding.lightNodeA.setOnClickListener(v ->  lightHideOrShow(0));
        detailBinding.lightNodeB.setOnClickListener(v -> lightHideOrShow(1));
        detailBinding.lightNodeC.setOnClickListener(v -> lightHideOrShow(2));
        detailBinding.lightNodeD.setOnClickListener(v -> lightHideOrShow(3));
        detailBinding.lightNodeE.setOnClickListener(v -> lightHideOrShow(4));
        detailBinding.lightNodeF.setOnClickListener(v -> lightHideOrShow(5));

        detailBinding.tempNodeA.setOnClickListener(v -> tempHideOrShow(0));
        detailBinding.tempNodeB.setOnClickListener(v -> tempHideOrShow(1));
        detailBinding.tempNodeC.setOnClickListener(v -> tempHideOrShow(2));
        detailBinding.tempNodeD.setOnClickListener(v -> tempHideOrShow(3));
        detailBinding.tempNodeE.setOnClickListener(v -> tempHideOrShow(4));

        DataMgr.LTSwitch.observe(getViewLifecycleOwner(), new Observer<Integer>() {
            @Override
            public void onChanged(Integer integer) {
                if (integer != null) {
                    if (integer == 1) {
                        detailBinding.realLightData.setVisibility(View.VISIBLE);
                        detailBinding.realTempData.setVisibility(View.GONE);
                        detailBinding.dataChartLight.setVisibility(View.VISIBLE);
                        detailBinding.dataChartTemp.setVisibility(View.GONE);
                    } else {
                        detailBinding.realLightData.setVisibility(View.GONE);
                        detailBinding.realTempData.setVisibility(View.VISIBLE);
                        detailBinding.dataChartLight.setVisibility(View.GONE);
                        detailBinding.dataChartTemp.setVisibility(View.VISIBLE);
                    }
                }
            }
        });
    }

    private void lightHideOrShow(int position) {
        switch (position) {
            case 0:
                detailBinding.lightImgA.setImageResource(R.drawable.ic_light_select);
                detailBinding.lightImgB.setImageResource(R.drawable.ic_light);
                detailBinding.lightImgC.setImageResource(R.drawable.ic_light);
                detailBinding.lightImgD.setImageResource(R.drawable.ic_light);
                detailBinding.lightImgE.setImageResource(R.drawable.ic_light);
                detailBinding.lightImgF.setImageResource(R.drawable.ic_light);
                setDeviceNum(0, true);
                break;
            case 1:
                detailBinding.lightImgA.setImageResource(R.drawable.ic_light);
                detailBinding.lightImgB.setImageResource(R.drawable.ic_light_select);
                detailBinding.lightImgC.setImageResource(R.drawable.ic_light);
                detailBinding.lightImgD.setImageResource(R.drawable.ic_light);
                detailBinding.lightImgE.setImageResource(R.drawable.ic_light);
                detailBinding.lightImgF.setImageResource(R.drawable.ic_light);
                setDeviceNum(1, true);
                break;
            case 2:
                detailBinding.lightImgA.setImageResource(R.drawable.ic_light);
                detailBinding.lightImgB.setImageResource(R.drawable.ic_light);
                detailBinding.lightImgC.setImageResource(R.drawable.ic_light_select);
                detailBinding.lightImgD.setImageResource(R.drawable.ic_light);
                detailBinding.lightImgE.setImageResource(R.drawable.ic_light);
                detailBinding.lightImgF.setImageResource(R.drawable.ic_light);
                setDeviceNum(2, true);
                break;
            case 3:
                detailBinding.lightImgA.setImageResource(R.drawable.ic_light);
                detailBinding.lightImgB.setImageResource(R.drawable.ic_light);
                detailBinding.lightImgC.setImageResource(R.drawable.ic_light);
                detailBinding.lightImgD.setImageResource(R.drawable.ic_light_select);
                detailBinding.lightImgE.setImageResource(R.drawable.ic_light);
                detailBinding.lightImgF.setImageResource(R.drawable.ic_light);
                setDeviceNum(3, true);
                break;
            case 4:
                detailBinding.lightImgA.setImageResource(R.drawable.ic_light);
                detailBinding.lightImgB.setImageResource(R.drawable.ic_light);
                detailBinding.lightImgC.setImageResource(R.drawable.ic_light);
                detailBinding.lightImgD.setImageResource(R.drawable.ic_light);
                detailBinding.lightImgE.setImageResource(R.drawable.ic_light_select);
                detailBinding.lightImgF.setImageResource(R.drawable.ic_light);
                setDeviceNum(4, true);
                break;
            case 5:
                detailBinding.lightImgA.setImageResource(R.drawable.ic_light);
                detailBinding.lightImgB.setImageResource(R.drawable.ic_light);
                detailBinding.lightImgC.setImageResource(R.drawable.ic_light);
                detailBinding.lightImgD.setImageResource(R.drawable.ic_light);
                detailBinding.lightImgE.setImageResource(R.drawable.ic_light);
                detailBinding.lightImgF.setImageResource(R.drawable.ic_light_select);
                setDeviceNum(5, true);
                break;
            default:
                break;
        }
    }

    private void tempHideOrShow(int position) {
        switch (position) {
            case 0:
                detailBinding.tempImgA.setImageResource(R.drawable.ic_temp_select);
                detailBinding.tempImgB.setImageResource(R.drawable.ic_temp);
                detailBinding.tempImgC.setImageResource(R.drawable.ic_temp);
                detailBinding.tempImgD.setImageResource(R.drawable.ic_temp);
                detailBinding.tempImgE.setImageResource(R.drawable.ic_temp);
                setDeviceNum(0, false);
                break;
            case 1:
                detailBinding.tempImgA.setImageResource(R.drawable.ic_temp);
                detailBinding.tempImgB.setImageResource(R.drawable.ic_temp_select);
                detailBinding.tempImgC.setImageResource(R.drawable.ic_temp);
                detailBinding.tempImgD.setImageResource(R.drawable.ic_temp);
                detailBinding.tempImgE.setImageResource(R.drawable.ic_temp);
                setDeviceNum(1, false);
                break;
            case 2:
                detailBinding.tempImgA.setImageResource(R.drawable.ic_temp);
                detailBinding.tempImgB.setImageResource(R.drawable.ic_temp);
                detailBinding.tempImgC.setImageResource(R.drawable.ic_temp_select);
                detailBinding.tempImgD.setImageResource(R.drawable.ic_temp);
                detailBinding.tempImgE.setImageResource(R.drawable.ic_temp);
                setDeviceNum(2, false);
                break;
            case 3:
                detailBinding.tempImgA.setImageResource(R.drawable.ic_temp);
                detailBinding.tempImgB.setImageResource(R.drawable.ic_temp);
                detailBinding.tempImgC.setImageResource(R.drawable.ic_temp);
                detailBinding.tempImgD.setImageResource(R.drawable.ic_temp_select);
                detailBinding.tempImgE.setImageResource(R.drawable.ic_temp);
                setDeviceNum(3, false);
                break;
            case 4:
                detailBinding.tempImgA.setImageResource(R.drawable.ic_temp);
                detailBinding.tempImgB.setImageResource(R.drawable.ic_temp);
                detailBinding.tempImgC.setImageResource(R.drawable.ic_temp);
                detailBinding.tempImgD.setImageResource(R.drawable.ic_temp);
                detailBinding.tempImgE.setImageResource(R.drawable.ic_temp_select);
                setDeviceNum(4, false);
                break;
            default:
                break;
        }
    }

    public void setDeviceNum(int deviceNum, boolean isLight) {
        this.deviceNum = deviceNum;
        Log.i(TAG, "setDeviceNum: " + deviceNum + "; isLight: " + isLight);
        if (detailBinding != null) {
            if (isLight) {
                LineData data = ChartMgr.getLightLineDataByDevice(deviceNum);
                detailBinding.dataChartLight.setData(data);
                detailBinding.dataChartLight.getDescription().setText("通道:No" + (deviceNum + 1));
            } else {
                LineData data = ChartMgr.getTempLineDataByDevice(deviceNum);
                detailBinding.dataChartTemp.setData(data);
                detailBinding.dataChartTemp.getDescription().setText("通道:No" + (deviceNum + 1));
            }
            initChart();
        }
    }

    private void registerChartRefresh() {
        DataMgr.getLightRefresh().observe(getViewLifecycleOwner(), aBoolean -> {
            dealLightData();
            initTableData();
        });
        DataMgr.getTempRefresh().observe(getViewLifecycleOwner(), aBoolean -> {
            dealTempData();
            initTableData();
        });
    }

    private void dealTempData() {
        if (detailBinding.dataChartTemp.getLineData() != null) {
            detailBinding.dataChartTemp.notifyDataSetChanged();
            detailBinding.dataChartTemp.setVisibleXRangeMaximum(30);
//            detailBinding.dataChartTemp.moveViewToX(detailBinding.dataChartTemp.getLineData().getEntryCount());
            detailBinding.dataChartTemp.moveViewTo(detailBinding.dataChartTemp.getLineData().getEntryCount(),
                    0f, YAxis.AxisDependency.RIGHT);
        } else {
            ChartMgr.getInstance().initChart(detailBinding.dataChartTemp, deviceNum, ChartMgr.TEMP_TYPE);
        }
    }

    private void dealLightData() {
        if (detailBinding.dataChartLight.getLineData() != null) {
            detailBinding.dataChartLight.notifyDataSetChanged();
            detailBinding.dataChartLight.setVisibleXRangeMaximum(30);
            detailBinding.dataChartLight.moveViewTo(detailBinding.dataChartLight.getLineData().getEntryCount(),
                    0f, YAxis.AxisDependency.RIGHT);
        } else {
            ChartMgr.getInstance().initChart(detailBinding.dataChartLight, deviceNum, ChartMgr.LIGHT_TYPE);
        }
    }

    private void initChart() {
        Log.i(TAG, "initChart: 初始化的时的deviceNum： " + deviceNum);
        // 0是光照类型
//        ChartMgr.getInstance().initChart(detailBinding.dataChartTemp, deviceNum, 0);
//        ChartMgr.getInstance().initChart(detailBinding.dataChartLight, deviceNum, 1);
        dealTempData();
        dealLightData();
        initTableData();
    }

    private void initTableData() {
        //数据表格填充
        Map<String, Float> dataMap = DataMgr.getDealData().get(deviceNum);
//        if (dataMap != null) {
//            detailBinding.miniTemp.setText(String.valueOf(dataMap.get(Constant.MINI_TEMP_DATA)));
//            detailBinding.maxTemp.setText(String.valueOf(dataMap.get(Constant.MAX_TEMP_DATA)));
//            detailBinding.avrTemp.setText(String.valueOf(dataMap.get(Constant.AVERAGE_TEMP_DATA)));
//            detailBinding.miniLight.setText(String.valueOf(dataMap.get(Constant.MINI_LIGHT_DATA)));
//            detailBinding.maxLight.setText(String.valueOf(dataMap.get(Constant.MAX_LIGHT_DATA)));
//            detailBinding.avrLight.setText(String.valueOf(dataMap.get(Constant.AVERAGE_Light_DATA)));
//        }
    }
}